package companySupSession;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import student.Student;
import student.StudentHome;

import company.Company;
import company.CompanyHome;
import compsup.CompanySup;
import compsup.CompanySupHome;

import degree.Degree;
import degree.DegreeHome;
import descriptor.CompanyDescriptor;
import descriptor.CompanySupDescriptor;
import descriptor.DegreeDescriptor;
import descriptor.DegreeListDescriptor;
import descriptor.FeedbackDescriptor;
import descriptor.StudentDescriptor;
import descriptor.StudentListDescriptor;
import extras.Md5Encryption;
import feedback.Feedback;
import feedback.FeedbackHome;

public class CompanySupSessionBean implements SessionBean{
	
	private CompanySup user = null;
	private boolean logged = false;
	
	public boolean logged()throws RemoteException{
		return logged;
	}
	public int log(int companySupId) throws RemoteException
	{
		InitialContext initialContext;
		try {
			initialContext = new InitialContext();
			CompanySupHome home = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
			CompanySup companySup = home.findByPrimaryKey(companySupId);
			logged = true;
			user = companySup;
			return 0;
		} catch (NamingException e) {
			e.printStackTrace();
			return -1;
		} catch (FinderException e) {
			e.printStackTrace();
			return -1;
		}
	}
	public int logout() throws RemoteException{
		if (logged){
			logged = false;
			user = null;
			return 0;
		}else{
			return -1;
		}
	}
	public CompanyDescriptor getCompanyDescriptor(int companyId) 
	throws RemoteException
	{
		try {
			InitialContext initialContext = new InitialContext();
			CompanyHome adminHome = (CompanyHome)initialContext.lookup("MyCompany_L");
			Company company;
			company = adminHome.findByPrimaryKey(companyId);
				return company.getDescriptor();
			} catch (FinderException e) {
				e.printStackTrace();
				return null;
			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			}
	}
	public CompanySupDescriptor getCompanySupDescriptor()
	throws RemoteException{
		if (logged){
			return user.toCompanySupDescriptor();
		}else{
			return null;
		}
		
	}
	public DegreeDescriptor getDegreeDescriptor(int degreeId) 
	throws RemoteException
	{
		if (logged){
			try {
				InitialContext initialContext = new InitialContext();
				DegreeHome home = (DegreeHome)initialContext.lookup("MyDegree_L");
				return home.findByPrimaryKey(degreeId).getDescriptor();
			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			} catch (FinderException e) {
				e.printStackTrace();
				return null;
			}
		}else{
			return null;
		}
	}
	public StudentDescriptor getStudentDescriptor(int studentId)
	throws RemoteException{
		if (logged){
			try {
				InitialContext initialContext = new InitialContext();
				StudentHome home = (StudentHome)initialContext.lookup("MyStudent_L");
				return home.findByPrimaryKey(studentId).getDescriptor();
			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			} catch (FinderException e) {
				e.printStackTrace();
				return null;
			}
		}else{
			return null;
		}
	}
	public FeedbackDescriptor getLinkedFeedBackOnDegree(int degreeId)
	throws RemoteException
	{
		if (logged){
			// check if permitted
			DegreeListDescriptor authorizedDegrees = this.getPersonnalDegreeDescriptors();
			boolean authorized = false;
			for (DegreeDescriptor dd:authorizedDegrees.getDegreeList()){
				if (dd.getId() == degreeId){
					authorized = true;
				}
			}
			if (authorized){
				try {
					InitialContext initialContext = new InitialContext();
					DegreeHome degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
					Degree degree = degreeHome.findByPrimaryKey(degreeId);
					Feedback feedback = null;
					for (Object o:degree.getFeedbacks()){
						Feedback f = (Feedback)o;
						if (f.getAuthor().getId() == user.getId()){
							return f.getDescriptor(); 
						}
					}
					return null;
				} catch (NamingException e) {
					e.printStackTrace();
					return null;
				} catch (FinderException e) {
					e.printStackTrace();
					return null;
				}
			}
			return null;
		}else{
			return null;	
		}
	}
	public FeedbackDescriptor getLinkedFeedBackOnStudent(int studentId)
	throws RemoteException
	{
		if (logged){
			// check if permitted
			StudentListDescriptor authorizedStudents = this.getPersonnalStudentDescriptors();
			boolean authorized = false;
			for (StudentDescriptor sd:authorizedStudents.getStudentList()){
				if (sd.getId() == studentId){
					authorized = true;
				}
			}
			if (authorized){
				try {
					InitialContext initialContext = new InitialContext();
					StudentHome studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
					Student student = studentHome.findByPrimaryKey(studentId);
					Feedback feedback = student.getCompanySupFeedback();
					if (feedback == null){
						return null;
					}
					if (feedback.getAuthor().getId() != user.getId()){
						return null;
					}else{
						return feedback.getDescriptor();
					}
					
				} catch (NamingException e) {
					e.printStackTrace();
					return null;
				} catch (FinderException e) {
					e.printStackTrace();
					return null;
				}
			}
			return null;
		}else{
			return null;
		}
		
	}
	
	public int editFeedBackOnDegree(int degreeId,String infos)
	throws RemoteException
	{
		if (logged){
			try {
				InitialContext initialContext = new InitialContext();
				DegreeHome home = (DegreeHome)initialContext.lookup("MyDegree_L");
				Degree degree = home.findByPrimaryKey(degreeId);
				Feedback theFeedBack = null;
				for (Object o: degree.getFeedbacks()){
					Feedback f = (Feedback)o;
					if (f.getAuthor().getId() == user.getId()){
						theFeedBack = f;
					}
				}
				if (theFeedBack == null){
					FeedbackHome feedbackHome = (FeedbackHome)initialContext.lookup("MyFeedback_L");
					theFeedBack = feedbackHome.create("Feedback",infos+"\n  by "+user.getFirstname()+" "+user.getSurname(),user.getUser());
					degree.getFeedbacks().add(theFeedBack);
					return 1;
				}else{
					theFeedBack.setInfos(infos+"\n  by "+user.getFirstname()+" "+user.getSurname());
					return 0;
				}
			} catch (NamingException e) {
				e.printStackTrace();
				return -2;
			} catch (FinderException e) {
				e.printStackTrace();
				return -3;
			} catch (CreateException e) {
				e.printStackTrace();
				return -4;
			}
		}else{
			return -1;
		}
	}
	public int editFeedBackOnStudent(int studentId,String infos)
	throws RemoteException
	{
		if (logged){
			try {
				InitialContext initialContext = new InitialContext();
				StudentHome home = (StudentHome)initialContext.lookup("MyStudent_L");
				Student student = home.findByPrimaryKey(studentId);
				FeedbackHome feedBackHome = (FeedbackHome)initialContext.lookup("MyFeedback_L");
				Feedback theFeedBack = feedBackHome.create("Company Supervisor Feedback",infos+"\n  by "+user.getFirstname()+" "+user.getSurname(),user.getUser());
				student.setCompanySupFeedback(theFeedBack);
				return 0;
			} catch (NamingException e) {
				e.printStackTrace();
				return -2;
			} catch (FinderException e) {
				e.printStackTrace();
				return -3;
			} catch (CreateException e) {
				e.printStackTrace();
				return -4;
			}
		}else{
			return -1;
		}
	}
	public StudentListDescriptor getPersonnalStudentDescriptors()
	throws RemoteException
	{
		if (logged){
			StudentListDescriptor students = new StudentListDescriptor();
			for (Object o: user.getStudents()){
				Student s = (Student)o;
				students.addStudent(s.getDescriptor());
			}
			return students;
		}else{
			return null;
		}
	}
	public DegreeListDescriptor getPersonnalDegreeDescriptors()
	throws RemoteException
	{
		if (logged){
			DegreeListDescriptor degrees = new DegreeListDescriptor();
			for (Object o: user.getStudents()){
				Student s = (Student)o;
				degrees.addDegree(s.getDegree().getDescriptor());
			}
			return degrees;
		}else{
			return null;
		}
		
	}		
	public int editInfos(String oldPassword,String password, String email)
	throws RemoteException
	{
		if (logged){
			user.setEmail(email);
			if (oldPassword != null){
				String hashedOld = Md5Encryption.getEncodedPassword(oldPassword);
				if (hashedOld.equalsIgnoreCase(user.getPassword())){
					user.setPassword(Md5Encryption.getEncodedPassword(password));
				}else{
					return -2;
				}
			}
			return 0;
		}
		return -1;
	}
	
	
	
	public int addAStudent(int studentId) 
	throws RemoteException
	{
		if (logged){
			// check if already present
			for (Object o: user.getStudents()){
				Student s = (Student)o;
				if (s.getId() == studentId){
					return -2;
				}
			}
			try {
				InitialContext initialContext = new InitialContext();
				StudentHome studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
				Student student = studentHome.findByPrimaryKey(studentId);
				user.getStudents().add(student);
				student.setCompanySup(user);
				FeedbackHome feedbackHome = (FeedbackHome)initialContext.lookup("MyFeedback_L");
				Feedback feedback = feedbackHome.create("Feedback"," ",user.getUser());
				student.setCompanySupFeedback(feedback);
				return 0;
			} catch (NamingException e) {
				e.printStackTrace();
				return -3;
			} catch (FinderException e) {
				e.printStackTrace();
				return -4;
			} catch (CreateException e) {
				e.printStackTrace();
				return -5;
			}
		}else{
			return -1;
		}
	}
	
	
	
	
	public String ejbCreate(){
		return null;
	}
	
	public void ejbPostCreate(){
		
	}
	
	public void ejbActivate() throws EJBException, RemoteException {
		
	}
	
	public void ejbPassivate() throws EJBException, RemoteException {
		
	}
	
	public void ejbRemove() throws EJBException, RemoteException {
		
	}
	
	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
		
	}
	
	
	
	
	
}
